﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public partial class ParticlesController : MonoBehaviour
{
    [SerializeField] Particle[] particles;

    private static Dictionary<int, Particle> registerParticles = new Dictionary<int, Particle>();

    private static List<ParticleCase> activeParticles = new List<ParticleCase>();
    private static int activeParticlesCount = 0;

    public void Initialise()
    {
        // Register particles
        for (int i = 0; i < particles.Length; i++)
        {
            RegisterParticle(particles[i]);
        }

        StartCoroutine(CheckForActiveParticles());
    }

    private IEnumerator CheckForActiveParticles()
    {
        while(true)
        {
            yield return null;
            yield return null;
            yield return null;
            yield return null;
            yield return null;

            for(int i = activeParticlesCount - 1; i >= 0; i--)
            {
                if (activeParticles[i].IsForceDisabledRequired())
                    activeParticles[i].ParticleSystem.Stop();

                if(!activeParticles[i].ParticleSystem.IsAlive())
                {
                    activeParticles[i].OnDisable();

                    activeParticles.RemoveAt(i);
                    activeParticlesCount--;
                }
            }
        }
    }

    public static ParticleCase ActivateParticle(Particle particle)
    {
        ParticleCase particleCase = new ParticleCase(particle);

        activeParticles.Add(particleCase);
        activeParticlesCount++;

        return particleCase;
    }

    #region Register
    public static int RegisterParticle(Particle particle)
    {
        int particleHash = particle.ParticleName.GetHashCode();
        if(!registerParticles.ContainsKey(particleHash))
        {
            particle.Initialise();

            registerParticles.Add(particle.ParticleName.GetHashCode(), particle);

            return particleHash;
        }
        else
        {
            Debug.LogError(string.Format("[Particle Controller]: Particle with name {0} already register!"));
        }

        return -1;
    }

    public static int RegisterParticle(string particleName, GameObject particlePrefab)
    {
        return RegisterParticle(new Particle(particleName, particlePrefab));
    }
    #endregion

    #region Play
    public static ParticleCase PlayParticle(string particleName)
    {
        int particleHash = particleName.GetHashCode();

        if (registerParticles.ContainsKey(particleHash))
        {
            return ActivateParticle(registerParticles[particleHash]);
        }

        Debug.LogError(string.Format("[Particles System]: Particle with type {0} is missing!", particleName));

        return null;
    }

    public static ParticleCase PlayParticle(int particleHash)
    {
        if (registerParticles.ContainsKey(particleHash))
        {
            return ActivateParticle(registerParticles[particleHash]);
        }

        Debug.LogError(string.Format("[Particles System]: Particle with hash {0} is missing!", particleHash));

        return null;
    }
    #endregion

    public static int GetHash(string particleName)
    {
        return particleName.GetHashCode();
    }
}
